﻿Imports DevExpress.XtraEditors.Controls
'OutStandingIssue: Should Go in WinFormExt/Form - aleblanc - 2011-12-19

Public Enum ChooserMode As Integer
    MetaCommand = 0
    ReportQuery = 1
End Enum

Public Class MqChooser
    Public Property QueryName() As String = Nothing
    Public Property ReportName() As String = Nothing
    Public Property ReportID() As Guid = Guid.Empty
    Public Property QueryID() As Guid = Guid.Empty
    Public Property Mode() As ChooserMode = ChooserMode.MetaCommand

    Private Sub MqChooser_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim mq As MetaQuery
        Select Case Mode
            Case ChooserMode.ReportQuery
                'OutStandingIssue: To Translate
                Me.LabelTitle.Text = "Choose Report"
                Me.Text = "Open Report"
                mq = MetaQuery.GetMetaQueryByName("ReportInDB")
                If mq IsNot Nothing Then
                    mq.FillResultQuery()
                    'TODO: LookupEdit Metatiser
                    lookupMQ.Properties.DataSource = mq.ResultQuery
                    lookupMQ.Properties.ValueMember = "Name"
                    lookupMQ.Properties.DisplayMember = "Name"
                    lookupMQ.Properties.PopulateColumns()
                    'Ici cacher le la colonne DefaultText car trop large et cache mon StringCD
                    'Quand ca sera metatiser la colonne sera visible = 0
                    lookupMQ.Properties.Columns("Data").Visible = False
                    lookupMQ.Properties.Columns("DocumentTemplateID").Visible = False
                    lookupMQ.Properties.BestFitMode = BestFitMode.BestFitResizePopup
                    lookupMQ.Properties.SearchMode = SearchMode.AutoFilter
                    lookupMQ.Properties.AutoSearchColumnIndex = lookupMQ.Properties.Columns.IndexOf(lookupMQ.Properties.Columns("StringCD"))
                    '
                    lookupMQ.EditValue = mq.ResultQuery.Rows(0).Item("Name").ToString()
                End If
            Case ChooserMode.MetaCommand
                'OutStandingIssue: To Translate
                Me.LabelTitle.Text = "Choose MetaCommand"
                Me.Text = "MetaCommand"
                mq = MetaQuery.GetMetaQueryByName("ReportQuery")
                If mq IsNot Nothing Then
                    mq.FillResultQuery()
                    'TODO: LookupEdit Metatiser
                    lookupMQ.Properties.DataSource = mq.ResultQuery
                    lookupMQ.Properties.ValueMember = "QueryName"
                    lookupMQ.Properties.DisplayMember = "QueryName"
                    lookupMQ.Properties.PopulateColumns()
                    lookupMQ.Properties.Columns("QueryID").Visible = False
                    lookupMQ.Properties.BestFitMode = BestFitMode.BestFitResizePopup
                    lookupMQ.Properties.SearchMode = SearchMode.AutoFilter
                    lookupMQ.Properties.AutoSearchColumnIndex = lookupMQ.Properties.Columns.IndexOf(lookupMQ.Properties.Columns("QueryName"))
                    '
                    lookupMQ.EditValue = mq.ResultQuery.Rows(0).Item("QueryName").ToString()
                End If
        End Select
    End Sub

    Private Sub lookupMQ_EditValueChanged(sender As System.Object, e As System.EventArgs) Handles lookupMQ.EditValueChanged
        Select Case Mode
            Case ChooserMode.ReportQuery
                ReportName = lookupMQ.EditValue.ToString
                ReportID = Guid.Parse(lookupMQ.GetColumnValue("DocumentTemplateID").ToString)

                If Not String.IsNullOrEmpty(lookupMQ.GetColumnValue("QueryID_FK").ToString) Then
                    QueryID = Guid.Parse(lookupMQ.GetColumnValue("QueryID_FK").ToString)
                End If
            Case ChooserMode.MetaCommand
                QueryName = lookupMQ.GetColumnValue("QueryName").ToString
                QueryID = Guid.Parse(lookupMQ.GetColumnValue("QueryID").ToString)
        End Select
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancl_Click(sender As System.Object, e As System.EventArgs) Handles btnCancl.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class
